<template>
  <div class="ai-chat-container">
    <!-- 聊天窗口 -->
    <div class="chat-window" :class="{ 'chat-window--minimized': minimized }" v-if="visible">
      <!-- 聊天头部 -->
      <div class="chat-header" @click="toggleMinimize">
        <div class="chat-title">
          <div class="chat-avatar">
            <el-icon><ChatDotRound /></el-icon>
          </div>
          <div class="title-info">
            <span class="title-text">AI助手</span>
            <span class="status-indicator" :class="{ 'online': !isTyping }">
              {{ isTyping ? '正在输入...' : '在线' }}
            </span>
          </div>
        </div>
        <div class="chat-controls">
          <button class="control-btn minimize-btn" @click.stop="toggleMinimize" :title="minimized ? '展开' : '最小化'">
            <i class="control-icon">{{ minimized ? '⬆' : '⬇' }}</i>
          </button>
          <button class="control-btn close-btn" @click.stop="closeChat" title="关闭">
            <i class="control-icon">✕</i>
          </button>
        </div>
      </div>

      <!-- 聊天内容区域 -->
      <div class="chat-content" v-show="!minimized">
        <!-- 未登录提示 -->
        <div class="login-prompt" v-if="!isLoggedIn">
          <div class="login-content">
            <div class="login-icon">
              <i>🔐</i>
            </div>
            <h3 class="login-title">请先登录</h3>
            <p class="login-desc">登录后即可与AI助手开始对话</p>
            <button class="login-btn" @click="handleLogin">
              <span>立即登录</span>
              <i class="login-arrow">→</i>
            </button>
          </div>
        </div>

        <!-- 聊天界面 -->
        <div v-else class="chat-interface">
          <!-- 消息列表 -->
          <div class="messages-container" ref="messagesContainer">
            <div
                v-for="(message, index) in messages"
                :key="message.id || index"
                class="message"
                :class="[message.type, { 'message-animate': index === messages.length - 1 }]"
            >
              <div class="message-avatar">
                <div class="avatar-circle" :class="message.type">
                  <i v-if="message.type === 'user'" class="avatar-icon">👤</i>
                  <i v-else class="avatar-icon">🤖</i>
                </div>
              </div>
              <div class="message-content">
                <div class="message-bubble" :class="message.type">
                  <div class="message-text" v-if="message.content" v-html="formatMessage(message.content)"></div>
                  <div class="message-loading" v-else-if="message.type === 'ai' && isTyping">
                    <div class="loading-container">
                      <div class="loading-wave">
                        <div class="wave-bar"></div>
                        <div class="wave-bar"></div>
                        <div class="wave-bar"></div>
                        <div class="wave-bar"></div>
                        <div class="wave-bar"></div>
                      </div>
                      <span class="loading-text">AI正在思考中</span>
                    </div>
                  </div>
                </div>
                <div class="message-time">{{ formatTime(message.timestamp) }}</div>
              </div>
            </div>

            <!-- 正在输入指示器 -->
            <div v-if="isTyping && !hasStreamingMessage" class="message ai typing-message">
              <div class="message-avatar">
                <div class="avatar-circle ai">
                  <i class="avatar-icon">🤖</i>
                </div>
              </div>
              <div class="message-content">
                <div class="message-bubble ai">
                  <div class="typing-indicator">
                    <div class="typing-wave">
                      <div class="wave-bar"></div>
                      <div class="wave-bar"></div>
                      <div class="wave-bar"></div>
                      <div class="wave-bar"></div>
                      <div class="wave-bar"></div>
                    </div>
                    <span class="typing-text">连接中...</span>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 输入区域 -->
          <div class="input-area">
            <div class="input-container">
              <div class="input-wrapper">
                <textarea
                    v-model="inputMessage"
                    @keydown="handleKeyDown"
                    @input="adjustTextareaHeight"
                    placeholder="输入您的问题..."
                    class="message-input"
                    :disabled="isTyping"
                    ref="messageInput"
                    rows="1"
                ></textarea>
                <div class="input-actions">
                  <button
                      @click="sendMessage"
                      class="send-btn"
                      :disabled="!inputMessage.trim() || isTyping"
                      :class="{ 'send-btn--active': inputMessage.trim() && !isTyping }"
                  >
                    <el-icon class="send-icon"><Top /></el-icon>
                  </button>
                </div>
              </div>
            </div>
            <div class="input-hint">
              按 Enter 发送，Ctrl + Enter 换行
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 悬浮按钮 -->
    <div class="chat-toggle-btn" @click="toggleChat" v-if="!visible">
      <div class="toggle-btn-inner">
        <el-icon class="toggle-icon"><ChatDotRound /></el-icon>
        <div class="pulse-ring"></div>
      </div>
      <div class="tooltip">点击开始聊天</div>
    </div>
  </div>
</template>

<script setup>
import { ref,nextTick, watch, computed, onMounted, onUnmounted } from 'vue'
import { aiChatStream } from '@/api/ai/ai'
import { ChatDotRound, Top } from '@element-plus/icons-vue'
import { useTokenStore } from '@/stores/token'
import { useRouter } from 'vue-router'

// 响应式数据
const visible = ref(false)
const minimized = ref(false)
const inputMessage = ref('')
const isTyping = ref(false)
const messagesContainer = ref(null)
const messageInput = ref(null)
const messages = ref([])

// 获取token store和router
const tokenStore = useTokenStore()
const router = useRouter()

// 登录状态计算属性
const isLoggedIn = computed(() => {
  return !tokenStore.isTokenExpired
})

// 判断是否有正在流式传输的消息
const hasStreamingMessage = computed(() => {
  const lastMessage = messages.value[messages.value.length - 1]
  return lastMessage && lastMessage.type === 'ai' && isTyping.value
})

// 生成唯一ID
const generateId = () => {
  return Date.now().toString(36) + Math.random().toString(36).substr(2)
}

// 格式化消息内容（支持换行，防止XSS）
const formatMessage = (content) => {
  if (!content) return ''
  // 简单的HTML转义
  const escapeHtml = (str) => {
    const div = document.createElement('div')
    div.textContent = str
    return div.innerHTML
  }
  return escapeHtml(content).replace(/\n/g, '<br>')
}

// 格式化时间
const formatTime = (timestamp) => {
  if (!timestamp) return ''
  return new Date(timestamp).toLocaleTimeString('zh-CN', {
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 自动调整输入框高度
const adjustTextareaHeight = () => {
  const textarea = messageInput.value
  if (textarea) {
    textarea.style.height = 'auto'
    textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px'
  }
}

// 滚动到底部
const scrollToBottom = () => {
  if (messagesContainer.value) {
    const container = messagesContainer.value
    nextTick(() => {
      container.scrollTo({
        top: container.scrollHeight,
        behavior: 'smooth'
      })
    })
  }
}

// 切换聊天窗口显示/隐藏
const toggleChat = () => {
  visible.value = !visible.value
  if (visible.value) {
    nextTick(() => {
      scrollToBottom()
      messageInput.value?.focus()
    })
  }
}

// 切换最小化状态
const toggleMinimize = () => {
  minimized.value = !minimized.value
  if (!minimized.value) {
    nextTick(() => {
      scrollToBottom()
      messageInput.value?.focus()
    })
  }
}

// 关闭聊天窗口
const closeChat = () => {
  visible.value = false
  minimized.value = false
}

// 处理登录跳转
const handleLogin = () => {
  visible.value = false
  router.push('/user/auth')
}

// 处理键盘事件
const handleKeyDown = (event) => {
  if (event.key === 'Enter') {
    if (event.ctrlKey || event.metaKey) {
      // Ctrl+Enter 换行
      return
    } else {
      // Enter 发送
      event.preventDefault()
      sendMessage()
    }
  }
}

// 发送消息
const sendMessage = async () => {
  // 检查登录状态
  if (!isLoggedIn.value) {
    handleLogin()
    return
  }

  const message = inputMessage.value.trim()
  if (!message || isTyping.value) return

  try {
    // 添加用户消息
    const userMessage = {
      id: generateId(),
      type: 'user',
      content: message,
      timestamp: Date.now()
    }
    messages.value.push(userMessage)

    // 清空输入框并重置高度
    inputMessage.value = ''
    adjustTextareaHeight()

    await nextTick()
    scrollToBottom()
    isTyping.value = true

    // 调用AI接口
    const reader = await aiChatStream(message)
    const decoder = new TextDecoder('utf-8')
    let accumulatedContent = ''
    let assistantMessage = null
    // eslint-disable-next-line
    while (true) {
      const { value, done } = await reader.read()
      if (done) break

      // 累积新内容
      accumulatedContent += decoder.decode(value, { stream: true })

      // 如果是第一次接收到内容，创建AI消息
      if (!assistantMessage && accumulatedContent.trim()) {
        assistantMessage = {
          id: generateId(),
          type: 'ai',
          content: accumulatedContent,
          timestamp: Date.now()
        }
        messages.value.push(assistantMessage)
      } else if (assistantMessage) {
        // 更新现有消息内容
        const messageIndex = messages.value.findIndex(m => m.id === assistantMessage.id)
        if (messageIndex !== -1) {
          messages.value[messageIndex].content = accumulatedContent
        }
      }

      await nextTick()
      scrollToBottom()
    }

    // 如果整个过程中都没有收到内容，添加一个空响应提示
    if (!assistantMessage) {
      const emptyResponseMessage = {
        id: generateId(),
        type: 'ai',
        content: '抱歉，我没有收到完整的回复，请重试。',
        timestamp: Date.now()
      }
      messages.value.push(emptyResponseMessage)
    }
  } catch (error) {
    console.error('AI聊天错误:', error)

    // 添加错误消息
    const errorMessage = {
      id: generateId(),
      type: 'ai',
      content: '抱歉，发生了错误，请稍后重试。',
      timestamp: Date.now()
    }
    messages.value.push(errorMessage)
  } finally {
    isTyping.value = false
    await nextTick()
    scrollToBottom()
  }
}

// 初始化欢迎消息
const initWelcomeMessage = () => {
  if (messages.value.length === 0) {
    messages.value.push({
      id: generateId(),
      type: 'ai',
      content: '您好！我是AI助手，有什么可以帮助您的吗？',
      timestamp: Date.now()
    })
  }
}

// 监听消息变化，自动滚动
watch(messages, () => {
  scrollToBottom()
}, { deep: true })

// 监听登录状态变化
watch(isLoggedIn, (newValue) => {
  if (newValue) {
    initWelcomeMessage()
  }
})

// 组件挂载时初始化
onMounted(() => {
  if (isLoggedIn.value) {
    initWelcomeMessage()
  }
})

// 清理资源
onUnmounted(() => {
  // 如果有正在进行的请求，可以在这里取消
})
</script>

<style scoped>
.ai-chat-container {
  position: fixed;
  bottom: 20px;
  left: 20px;
  z-index: 9999;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* 悬浮按钮 */
.chat-toggle-btn {
  position: relative;
  width: 56px;
  height: 56px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.toggle-btn-inner {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: #1a1a1a;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  transition: all 0.3s ease;
  border: 2px solid #333;
}

.toggle-icon {
  font-size: 24px;
  z-index: 2;
  transition: all 0.3s ease;
}

.pulse-ring {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: #1a1a1a;
  animation: pulse 2s infinite;
  opacity: 0.6;
}

.tooltip {
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  background: #1a1a1a;
  color: white;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 12px;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  margin-left: 8px;
  border: 1px solid #333;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.tooltip::after {
  content: '';
  position: absolute;
  right: 100%;
  top: 50%;
  transform: translateY(-50%);
  border: 5px solid transparent;
  border-right-color: #1a1a1a;
}

.chat-toggle-btn:hover .toggle-btn-inner {
  transform: scale(1.1);
  background: #333;
  box-shadow: 0 6px 30px rgba(0, 0, 0, 0.4);
}

.chat-toggle-btn:active .toggle-btn-inner {
  transform: scale(0.95);
  background: linear-gradient(135deg, #444 0%, #666 100%);
}

.chat-toggle-btn:hover .tooltip {
  opacity: 1;
  visibility: visible;
}

@media (max-width: 900px) {
  .chat-toggle-btn {
    display: none;
  }
}

@media (max-height: 700px) {
  .chat-toggle-btn {
    display: none;
  }
}

/* 聊天窗口 */
.chat-window {
  width: 380px;
  height: 550px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15), 0 8px 32px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border: 2px solid #e5e7eb;
  opacity: 0;
  transform: translateY(20px) scale(0.9);
  animation: windowPopIn 0.4s ease-out forwards;
}

.chat-window--minimized {
  height: 70px;
}

/* 聊天头部 */
.chat-header {
  background: #1a1a1a;
  color: white;
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  border-bottom: 1px solid #333;
}

.chat-header:hover {
  background: #2a2a2a;
}

.chat-header:active {
  background: linear-gradient(135deg, #333 0%, #444 100%);
  transform: scale(0.98);
}

.chat-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #333;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: all 0.3s ease;
}

.chat-avatar:hover {
  background: #444;
  transform: scale(1.05);
}

.title-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.title-text {
  font-weight: 600;
  font-size: 16px;
}

.status-indicator {
  font-size: 12px;
  color: #999;
  transition: color 0.3s ease;
}

.status-indicator.online {
  color: #666;
}

.chat-controls {
  display: flex;
  gap: 8px;
}

.control-btn {
  background: #333;
  border: none;
  color: white;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.control-btn::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: linear-gradient(135deg, #666 0%, #888 100%);
  border-radius: 50%;
  transition: all 0.3s ease;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.control-btn:hover::before {
  width: 100%;
  height: 100%;
}

.control-btn:active {
  transform: scale(0.95);
}

.control-icon {
  font-size: 14px;
  font-style: normal;
  position: relative;
  z-index: 2;
}

.control-btn:hover {
  background: #444;
  transform: scale(1.05);
}

.close-btn:hover {
  background: #555;
}

.close-btn:hover::before {
  background: linear-gradient(135deg, #666 0%, #777 100%);
}

/* 聊天内容区域 */
.chat-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #fafafa;
}

/* 聊天界面容器 */
.chat-interface {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* 登录提示样式 */
.login-prompt {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  background: #fafafa;
}

.login-content {
  text-align: center;
  max-width: 280px;
}

.login-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  border-radius: 50%;
  background: #1a1a1a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  color: white;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  animation: loginIconFloat 3s ease-in-out infinite;
  transition: all 0.3s ease;
}

.login-icon:hover {
  background: #333;
  transform: scale(1.05);
}

.login-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 8px;
}

.login-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 24px;
  line-height: 1.5;
}

.login-btn {
  background: #1a1a1a;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: linear-gradient(135deg, #333 0%, #555 100%);
  border-radius: 12px;
  transition: all 0.3s ease;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.login-btn:hover::before {
  width: 120%;
  height: 120%;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
}

.login-btn:active {
  transform: translateY(0) scale(0.98);
}

.login-btn span,
.login-arrow {
  position: relative;
  z-index: 2;
}

.login-arrow {
  font-style: normal;
  transition: transform 0.3s ease;
}

.login-btn:hover .login-arrow {
  transform: translateX(4px);
}

@keyframes loginIconFloat {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-8px);
  }
}

/* 消息容器 */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

/* 消息样式 */
.message {
  display: flex;
  gap: 12px;
  max-width: 100%;
  opacity: 0;
  animation: messageSlideIn 0.4s ease-out forwards;
}

.message.user {
  flex-direction: row-reverse;
}

.message-avatar {
  flex-shrink: 0;
}

.avatar-circle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all 0.3s ease;
}

.avatar-circle.user {
  background: #1a1a1a;
  color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.avatar-circle.user:hover {
  background: #333;
  transform: scale(1.05);
}

.avatar-circle.ai {
  background: #666;
  color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
  position: relative;
}

.avatar-circle.ai:hover {
  background: #777;
  transform: scale(1.05);
}

/* AI正在思考时的脉冲效果 */
.typing-message .avatar-circle.ai,
.message:has(.message-loading) .avatar-circle.ai {
  animation: aiPulse 2s infinite ease-in-out;
}

.typing-message .avatar-circle.ai::after,
.message:has(.message-loading) .avatar-circle.ai::after {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  border: 2px solid #666;
  border-radius: 50%;
  animation: aiRing 2s infinite ease-in-out;
  opacity: 0;
}

.avatar-icon {
  font-style: normal;
}

.message-content {
  flex: 1;
  max-width: calc(100% - 48px);
}

.message-bubble {
  padding: 12px 16px;
  border-radius: 18px;
  word-wrap: break-word;
  line-height: 1.5;
  position: relative;
  transition: all 0.3s ease;
  min-height: 20px;
}

.message-bubble.user {
  background: #1a1a1a;
  color: white;
  margin-left: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  position: relative;
  overflow: hidden;
}

.message-bubble.user::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent);
  transition: left 0.5s ease;
}

.message-bubble.user:hover::before {
  left: 100%;
}

.message-bubble.ai {
  background: white;
  color: #1a1a1a;
  margin-right: 20px;
  border: 2px solid #e5e7eb;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: hidden;
}

.message-bubble.ai::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(0,0,0,0.03), transparent);
  transition: left 0.5s ease;
}

.message-bubble.ai:hover::before {
  left: 100%;
}

.message-bubble:hover {
  transform: translateY(-1px);
}

.message-bubble:active {
  transform: translateY(0) scale(0.98);
}

.message-text {
  font-size: 14px;
}

.message-loading {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  min-height: 40px;
  padding: 4px 0;
}

.loading-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.loading-wave {
  display: flex;
  align-items: center;
  gap: 3px;
  height: 20px;
}

.wave-bar {
  width: 3px;
  height: 6px;
  background: linear-gradient(135deg, #666 0%, #999 100%);
  border-radius: 2px;
  animation: wave 1.2s infinite ease-in-out;
  transform-origin: bottom;
}

.wave-bar:nth-child(1) { animation-delay: -0.4s; }
.wave-bar:nth-child(2) { animation-delay: -0.3s; }
.wave-bar:nth-child(3) { animation-delay: -0.2s; }
.wave-bar:nth-child(4) { animation-delay: -0.1s; }
.wave-bar:nth-child(5) { animation-delay: 0s; }

.loading-text {
  font-size: 12px;
  color: #666;
  font-style: italic;
  opacity: 0.8;
  animation: fadeInOut 2s infinite ease-in-out;
}

.loading-dots {
  display: flex;
  gap: 4px;
}

.loading-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #999;
  animation: loadingDots 1.4s infinite ease-in-out;
}

.loading-dots span:nth-child(1) {
  animation-delay: -0.32s;
}

.loading-dots span:nth-child(2) {
  animation-delay: -0.16s;
}

.message-time {
  font-size: 11px;
  color: #9ca3af;
  margin-top: 6px;
  opacity: 0.8;
}

.message.user .message-time {
  text-align: left;
}

.message.ai .message-time {
  text-align: right;
}

/* 输入区域 */
.input-area {
  padding: 20px;
  background: white;
  border-top: 1px solid #e5e7eb;
}

.input-container {
  margin-bottom: 8px;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  background: #f9fafb;
  border: 2px solid #e5e7eb;
  border-radius: 24px;
  padding: 8px 12px;
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  border-color: #1a1a1a;
  background: white;
  box-shadow: 0 0 0 3px rgba(26, 26, 26, 0.1);
}

.message-input {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  outline: none;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  min-height: 20px;
  max-height: 120px;
  padding: 6px 0;
  color: #1a1a1a;
}

.message-input::placeholder {
  color: #999;
}

.message-input:disabled {
  color: #999;
  cursor: not-allowed;
}

.input-actions {
  display: flex;
  align-items: center;
}

.send-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: #e5e7eb;
  color: #999;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}

.send-btn::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: linear-gradient(135deg, #333 0%, #555 100%);
  border-radius: 50%;
  transition: all 0.3s ease;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.send-btn--active {
  background: #1a1a1a;
  color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  transform: scale(1.05);
}

.send-btn--active::before {
  width: 100%;
  height: 100%;
}

.send-btn:disabled {
  cursor: not-allowed;
  transform: none;
}

.send-btn:active {
  transform: scale(0.95);
}

.send-icon {
  font-size: 16px;
  position: relative;
  z-index: 2;
}

.input-hint {
  font-size: 11px;
  color: #999;
  text-align: center;
  margin-top: 4px;
}

/* 正在输入指示器 */
.typing-message {
  animation: typingMessageSlideIn 0.3s ease-out;
}

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 0;
  min-height: 32px;
}

.typing-wave {
  display: flex;
  align-items: center;
  gap: 3px;
  height: 20px;
}

.typing-wave .wave-bar {
  width: 3px;
  height: 6px;
  background: linear-gradient(135deg, #888 0%, #aaa 100%);
  border-radius: 2px;
  animation: wave 1.2s infinite ease-in-out;
  transform-origin: bottom;
}

.typing-wave .wave-bar:nth-child(1) { animation-delay: -0.4s; }
.typing-wave .wave-bar:nth-child(2) { animation-delay: -0.3s; }
.typing-wave .wave-bar:nth-child(3) { animation-delay: -0.2s; }
.typing-wave .wave-bar:nth-child(4) { animation-delay: -0.1s; }
.typing-wave .wave-bar:nth-child(5) { animation-delay: 0s; }

.typing-text {
  font-size: 12px;
  color: #888;
  font-style: italic;
  opacity: 0.8;
  animation: fadeInOut 2s infinite ease-in-out;
}

/* 动画 */
@keyframes windowPopIn {
  0% {
    opacity: 0;
    transform: translateY(20px) scale(0.9);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 0.6;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.3;
  }
  100% {
    transform: scale(1);
    opacity: 0.6;
  }
}

@keyframes messageSlideIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes typingMessageSlideIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes wave {
  0%, 60%, 100% {
    transform: scaleY(1);
    opacity: 0.4;
  }
  30% {
    transform: scaleY(2.5);
    opacity: 1;
  }
}

@keyframes fadeInOut {
  0%, 100% {
    opacity: 0.6;
  }
  50% {
    opacity: 1;
  }
}

@keyframes typing {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes loadingDots {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 滚动条样式 */
.messages-container::-webkit-scrollbar {
  width: 6px;
}

.messages-container::-webkit-scrollbar-track {
  background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 3px;
  transition: background 0.3s ease;
}

.messages-container::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .ai-chat-container {
    bottom: 10px;
    left: 10px;
    right: 10px;
  }

  .chat-window {
    width: calc(100vw - 20px);
    height: calc(100vh - 40px);
    max-height: 600px;
  }

  .chat-header {
    padding: 12px 16px;
  }

  .title-text {
    font-size: 14px;
  }

  .messages-container {
    padding: 16px;
    gap: 12px;
  }

  .input-area {
    padding: 16px;
  }
}

@media (max-height: 600px) {
  .chat-window {
    height: calc(100vh - 40px);
    max-height: none;
  }
}
</style>